package com.micai.springboot.controller;

import com.micai.springboot.base.BaseController;
import com.micai.springboot.base.constant.Constants;
import com.micai.springboot.dto.MemberDto;
import com.micai.springboot.param.UserInfoUpdateParam;
import com.micai.springboot.service.MemberService;
import com.micai.springboot.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2017/11/13 18:03
 */
@Controller
@RequestMapping(value = "/user")
public class UserInfoController extends BaseController {

    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/main")
    public String main(HttpServletRequest request, Model model) {
        MemberDto memberDto = (MemberDto) sessionProvider.getAttribute(request, Constants.MEMBER_SESSION);
        if (memberDto != null) {
            model.addAttribute("member", memberService.getMemberById(memberDto.getId()));
        }
        return "front/userInfo/userMain";
    }

    @RequestMapping(value = "/info")
    public String info(HttpServletRequest request, Model model) {
        MemberDto memberDto = (MemberDto) sessionProvider.getAttribute(request, Constants.MEMBER_SESSION);
        if (memberDto != null) {
            model.addAttribute("member", memberService.getMemberById(memberDto.getId()));
        }
        return "front/userInfo/userInfo";
    }

    @RequestMapping(value = "/set")
    public String set(HttpServletRequest request, Model model) {
        MemberDto memberDto = (MemberDto) sessionProvider.getAttribute(request, Constants.MEMBER_SESSION);
        if (memberDto != null) {
            model.addAttribute("member", memberService.getMemberById(memberDto.getId()));
        }
        return "front/userInfo/userSet";
    }

    @RequestMapping(value = "/mes")
    public String mes(HttpServletRequest request, Model model) {
        MemberDto memberDto = (MemberDto) sessionProvider.getAttribute(request, Constants.MEMBER_SESSION);
        if (memberDto != null) {
            model.addAttribute("member", memberService.getMemberById(memberDto.getId()));
        }
        return "front/userInfo/userMes";
    }

    @RequestMapping(value = "/userInfoUpdate")
    @ResponseBody
    public Map<String, Object> userInfoUpdate(@ModelAttribute UserInfoUpdateParam userInfoUpdateParam) {
        Map<String, Object> retMap = null;
        try {
            MemberDto memberDto = memberService.getMemberById(userInfoUpdateParam.getId());
            if (memberDto != null) {
                memberDto.setName(userInfoUpdateParam.getName());
                memberDto.setPhone(userInfoUpdateParam.getPhone());
                memberDto.setEmail(userInfoUpdateParam.getEmail());
                if (memberService.updateMemberById(memberDto) == 1) {
                    retMap = MessageUtils.success("我的资料修改成功");
                    return retMap;
                }
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

}
